//
//  AesEncrypt.cpp
//  GameLaLaWork
//
//  Created by gamelala on 14-8-1.
//  Copyright (c) 2014年 gamelala. All rights reserved.
//

#include "AesEncrypt.h"


static const char key_common[16]=
{
	'G','_','^','L','^','8','*','<',
	'0','+','l','f','@','c','6','>'
};

static const char key_network[16]=
{
	'^','_','s','T','<','>','-','#',
	'g','+','L','L','@','8','*','>'
};

static const char key_network_de[16]=
{
	'+','G','@','L','l','*','<','>',
	'-','#','y','+','W','b','@','a'   
};

void AesEncrypt::copyData(uint8 *des,const uint8* src, int32 len)
{
	int32 index = 0;
	for(index=0; index<len; index++)
	{
		des[index] = (uint8)src[index];
        
        
	}
    
}

uint32 AesEncrypt::getDataLen(const char* buf,int32 bufLen)
{
	uint32 count = bufLen;
	while((count > bufLen-16) && buf[count-1] == 0){
		count --;
	}
	return count;
}


unsigned char* AesEncrypt::encryptAES(const char* key, int keyLen, BYTE input[],int size)
{
	int bufLen = 0;
    unsigned char * pIn = NULL;
    BYTE* pInput = &(input[0]);
    
    int32 pInputLen= size;
    
	if(NULL == pInput || pInputLen == 0) {
		
        return NULL;
	}
    
	bufLen = ((pInputLen % 16) == 0) ?  pInputLen: (((pInputLen>>4)+1)<<4);
    
	pIn = (uint8*)malloc(bufLen);
	if(pIn){
		memset(pIn, 0, bufLen);
		copyData(pIn,pInput,MIN(bufLen,pInputLen));
	}else{
        
        
		return NULL;
	}
    
    {
        aes_context ctx = {0};
        unsigned char tmpKey[32] = {0};
        unsigned char iv[16] = {0};
        
        memcpy(tmpKey,key,MIN(sizeof(tmpKey),keyLen));
        
        
        aes_setkey_enc( &ctx, tmpKey, 128);
        
        
        
        aes_crypt_cbc( &ctx, AES_ENCRYPT, bufLen, iv, pIn, pIn );
    }
    

    return pIn;
    
    
}


unsigned char* AesEncrypt ::decryptAES(const char* key,int keyLen,BYTE input[],int size,int *outLen)
{
    int bufLen = 0;
   
    unsigned char * pIn = 0;
    BYTE* pInput = &(input[0]);
    int32 pInputLen= size;
    uint32 bufDataLen = 0;
    if(NULL == pInput || pInputLen == 0) {
		return NULL;
	}
    
    bufLen = ((pInputLen % 16) == 0) ?  pInputLen: (((pInputLen>>4)+1)<<4);
	pIn = (uint8*)malloc(bufLen);
	if(pIn){
		memset(pIn, 0, bufLen);
		copyData(pIn,pInput,MIN(bufLen,pInputLen));
        
	}else{
		return NULL;
	}
    {
        aes_context ctx = {0};
        unsigned char tmpKey[32] = {0};
        unsigned char iv[16] = {0};
        
        memcpy(tmpKey,key,MIN(sizeof(tmpKey),keyLen));
        
        aes_setkey_dec( &ctx, tmpKey, 128);
        aes_crypt_cbc( &ctx, AES_DECRYPT, bufLen, iv, pIn, pIn);
        
    }
    
 
    
   // bufDataLen = MIN(bufDataLen, getDataLen((const char*)pIn, bufLen));
//    printf("len-getDataLenn:%u\n", getDataLen((const char*)pIn, bufLen));
//    printf("len-bufLen:%u\n", bufLen);
//    printf("len-bufDataLen:%u\n", bufDataLen);
//    printf("len-pIn:%s\n", pIn);
//    //pIn[bufLen]=0;
//    printf("len-pIns:%s\n", pIn);
    
    *outLen = getDataLen((const char*)pIn, bufLen);
    
    return pIn;
}


@implementation AesEncrypt_IOS
+(NSString*)encryptAESAndBase64:(const char* )key  ipunt:(BYTE*) input
{
    
    //    int keyLen = strlen(key);
    //    int size = strlen((const char*)input);
    unsigned char* endoce =  AesEncrypt::encryptAES(key,strlen(key),input,strlen((const char*)input));
    printf("endoce＋＋＋＋:%s\n",endoce);
    int len = strlen((const char*)input);
    len = ((len + 15) / 16) * 16;
    printf("padding:%d\n", len);
    NSData* originDatae = [NSData dataWithBytes:(void*)endoce length:len];
    printf("len-endoce:%zu\n", strlen((const char*)endoce));
    NSString *resault = [originDatae EncodedString];  //加密的结果
    NSLog(@"-resault------%@",resault);
    return resault;
    
}


+(NSString*)DecCodebase64decryptAES:(const char* )key input:(NSString *)input
{

    NSData *enData = [NSData dataBase64String:input]; //把加密的结果再解密
    
    NSLog(@"xml---%lu",(unsigned long)[enData length]);
    
    unsigned char*cdaa = (unsigned char*)[enData bytes];

   // printf("len-endoce:%zu\n", strlen((const char*)cdaa));
    
    
    int outLen =0;
    unsigned char *decode = AesEncrypt::decryptAES(key, strlen(key), (BYTE*)cdaa, (unsigned long)[enData length],& outLen);
    
   NSString *str=[[NSString alloc] initWithBytes:decode length:outLen encoding:NSUTF8StringEncoding];
    free(decode);
    
    return str;
    
}
@end
